package calcs;

import static util.UserInputChecker.checkInput;
import static util.UserInputChecker.setCalculated;
import static util.UserInputChecker.setInitial;
import gui.ErrorMessage;
import gui.MyFrame;
import gui.data.D2.CircleOutputGui;
import static util.Util.*;

public class CircleCalculator extends Thread {
	double radius, diameter, circumference, area, arc, arcArea, arcAngle;
	double pi = Math.PI;
	MyFrame myFrame;
	boolean found = false, before = false, calculated = false;

	public CircleCalculator(double radius, double diameter,
			double circumference, double area, double arc, double arcArea,
			double arcAngle, final MyFrame myFrame) {
		this.myFrame = myFrame;
		this.radius = radius;
		this.area = area;
		this.diameter = diameter;
		this.circumference = circumference;
		this.arc = arc;
		this.arcArea = arcArea;
		this.arcAngle = arcAngle;
		setInitial(radius, diameter, circumference, area, arc, arcArea,
				arcAngle);
		start();
	}
	
	public CircleCalculator(double radius, double diameter,
			double circumference, double area, double arc, double arcArea,
			double arcAngle) {
		this.radius = radius;
		this.area = area;
		this.diameter = diameter;
		this.circumference = circumference;
		this.arc = arc;
		this.arcArea = arcArea;
		this.arcAngle = arcAngle;
	}

	public void run() {
		do {
			found = false;
			/*before = false;
			calculated = false;

			before = radius != 0;
			calculateRadius();
			if (!before && found) {
				calculated = true;
			}

			before = diameter != 0;
			calculateDiameter();
			if (!before && found) {
				calculated = true;
			}

			before = circumference != 0;
			calculateCircumference();
			if (!before && found) {
				calculated = true;
			}

			before = area != 0;
			calculateArea();
			if (!before && found) {
				calculated = true;
			}

			before = arc != 0;
			calculateArc();
			if (!before && found) {
				calculated = true;
			}

			before = arcArea != 0;
			calculateArcArea();
			if (!before && found) {
				calculated = true;
			}

			before = arcAngle != 0;
			calculateArcAngle();
			if (!before && found) {
				calculated = true;
			}*/
			if(radius == 0){
				calculateRadius();
			}
			if(diameter == 0){
				calculateDiameter();
			}
			if(circumference == 0){
				calculateCircumference();
			}
			if(area == 0){
				calculateArea();
			}
			if(arc == 0){
				calculateArc();
			}
			if(arcArea == 0){
				calculateArcArea();
			}
			if(arcAngle == 0){
				calculateArcAngle();
			}
		} while (found);

		radius = roundToDecimals(radius, 3);
		diameter = roundToDecimals(diameter, 3);
		circumference = roundToDecimals(circumference, 3);
		area = roundToDecimals(area, 3);
		arc = roundToDecimals(arc, 3);
		arcArea = roundToDecimals(arcArea, 3);
		arcAngle = roundToDecimals(arcAngle, 3);
		if(myFrame != null){
			setCalculated(radius, diameter, circumference, area, arc, arcArea,
					arcAngle);
		//if (checkInput()) {
		
			myFrame.setDataPanel(new CircleOutputGui(radius, diameter,
					circumference, area, arc, arcArea, arcAngle),
					"CIRCLE_OUTPUT");
		}
		//} else {
		//	new ErrorMessage("Check input");
		//}
	}

	private void calculateArcAngle() {
		if (radius != 0) {
			if (arc != 0) {
				arcAngle = arc / radius;
				found = true;
			} else if (arcArea != 0) {
				arcAngle = Math.pow(radius, 2) / (2 * arcArea);
				found = true;
			}
		}
	}

	private void calculateArcArea() {
		if (radius != 0) {
			if (arcAngle != 0) {
				arcArea = (arcAngle / 2) * Math.pow(radius, 2);
				found = true;
			} else if (arc != 0) {
				arcArea = (arc * radius) / 2;
				found = true;
			}
		}
	}

	private void calculateArc() {
		if (radius != 0) {
			if (arcAngle != 0) {
				arc = radius * arcAngle;
				found = true;
			} else if (arcArea != 0) {
				arc = (2 * arcArea) / radius;
				found = true;
			}
		}
	}

	private void calculateArea() {
		if (diameter != 0) {
			area = pi * Math.pow((diameter / 2), 2);
			found = true;
		} else if (circumference != 0) {
			area = Math.pow(circumference, 2) / (4 * pi);
			found = true;
		} else if (radius != 0) {
			area = pi * Math.pow(radius, 2);
			found = true;
		}
	}

	private void calculateCircumference() {
		if (diameter != 0) {
			circumference = pi * diameter;
			found = true;
		} else if (area != 0) {
			circumference = Math.sqrt((area * 4 * pi));
			found = true;
		} else if (radius != 0) {
			circumference = 2 * pi * radius;
			found = true;
		}
	}

	private void calculateDiameter() {
		if (radius != 0) {
			diameter = radius * 2;
			found = true;
		} else if (area != 0) {
			diameter = (Math.sqrt((area / pi))) * 2;
			found = true;
		} else if (circumference != 0) {
			diameter = circumference / pi;
			found = true;
		}
	}

	private void calculateRadius() {
		if (diameter != 0) {
			radius = diameter / 2;
			found = true;
		} else if (area != 0) {
			radius = Math.sqrt((area / pi));
			found = true;
		} else if (circumference != 0) {
			radius = circumference / (2 * pi);
			found = true;
		} else if (arcArea != 0 && arcAngle != 0) {
			radius = Math.sqrt((2 * arcArea) / arcAngle);
			found = true;
		} else if (arcArea != 0 && arc != 0) {
			radius = (2 * arcArea) / arc;
			found = true;
		} else if (arc != 0 && arcAngle != 0) {
			radius = arc / arcAngle;
			found = true;
		}
	}
}
